package handlers

import (
	"context"
	"ourplansystem/server/ent"
	"ourplansystem/server/models"
	"strconv"
	"time"

	"github.com/gofiber/fiber/v2"
)

// WishHandler 愿望清单处理器
type WishHandler struct {
	client *ent.Client
}

// NewWishHandler 创建愿望清单处理器
func NewWishHandler(client *ent.Client) *WishHandler {
	return &WishHandler{client: client}
}

// CreateWishRequest 创建愿望请求
type CreateWishRequest struct {
	Content   string `json:"content"`
	CreatorID int64  `json:"creator_id"`
}

// UpdateWishRequest 更新愿望请求
type UpdateWishRequest struct {
	Content     string `json:"content"`
	IsCompleted bool   `json:"is_completed"`
}

// GetWishes 获取所有愿望
func (h *WishHandler) GetWishes(c *fiber.Ctx) error {
	ctx := context.Background()
	wishes, err := h.client.Wish.Query().WithCreator().All(ctx)
	if err != nil {
		return models.InternalServerError(c, "获取愿望清单失败")
	}

	return models.Success(c, wishes)
}

// GetWish 获取单个愿望
func (h *WishHandler) GetWish(c *fiber.Ctx) error {
	ctx := context.Background()
	id, err := strconv.ParseInt(c.Params("id"), 10, 64)
	if err != nil {
		return models.BadRequest(c, "无效的愿望ID")
	}

	wish, err := h.client.Wish.Get(ctx, id)
	if err != nil {
		if ent.IsNotFound(err) {
			return models.NotFound(c, "愿望不存在")
		}
		return models.InternalServerError(c, "获取愿望失败")
	}

	return models.Success(c, wish)
}

// CreateWish 创建愿望
func (h *WishHandler) CreateWish(c *fiber.Ctx) error {
	ctx := context.Background()
	var req CreateWishRequest
	if err := c.BodyParser(&req); err != nil {
		return models.BadRequest(c, "请求参数错误")
	}

	if req.Content == "" {
		return models.BadRequest(c, "内容不能为空")
	}

	if req.CreatorID == 0 {
		return models.BadRequest(c, "创建者ID不能为空")
	}

	wish, err := h.client.Wish.
		Create().
		SetContent(req.Content).
		SetCreatorID(req.CreatorID).
		Save(ctx)

	if err != nil {
		return models.InternalServerError(c, "创建愿望失败")
	}

	return models.SuccessWithMessage(c, "创建成功", wish)
}

// UpdateWish 更新愿望
func (h *WishHandler) UpdateWish(c *fiber.Ctx) error {
	ctx := context.Background()
	id, err := strconv.ParseInt(c.Params("id"), 10, 64)
	if err != nil {
		return models.BadRequest(c, "无效的愿望ID")
	}

	var req UpdateWishRequest
	if err := c.BodyParser(&req); err != nil {
		return models.BadRequest(c, "请求参数错误")
	}

	builder := h.client.Wish.UpdateOneID(id)

	if req.Content != "" {
		builder.SetContent(req.Content)
	}

	// 如果标记为完成，设置完成时间
	builder.SetIsCompleted(req.IsCompleted)
	if req.IsCompleted {
		now := time.Now()
		builder.SetCompletedAt(now)
	} else {
		builder.ClearCompletedAt()
	}

	wish, err := builder.Save(ctx)
	if err != nil {
		if ent.IsNotFound(err) {
			return models.NotFound(c, "愿望不存在")
		}
		return models.InternalServerError(c, "更新愿望失败")
	}

	return models.SuccessWithMessage(c, "更新成功", wish)
}

// DeleteWish 删除愿望
func (h *WishHandler) DeleteWish(c *fiber.Ctx) error {
	ctx := context.Background()
	id, err := strconv.ParseInt(c.Params("id"), 10, 64)
	if err != nil {
		return models.BadRequest(c, "无效的愿望ID")
	}

	err = h.client.Wish.DeleteOneID(id).Exec(ctx)
	if err != nil {
		if ent.IsNotFound(err) {
			return models.NotFound(c, "愿望不存在")
		}
		return models.InternalServerError(c, "删除愿望失败")
	}

	return models.SuccessWithMessage(c, "删除成功", nil)
}
